package com.example.simpleforum.servlet;

import com.example.simpleforum.dao.ForumApplicationDao;
import com.example.simpleforum.dao.ForumDao;
import com.example.simpleforum.model.ForumApplication;
import com.example.simpleforum.model.User;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 板块申请Servlet
 */
public class ForumApplicationServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 获取当前登录用户
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        
        if (user == null) {
            // 用户未登录，重定向到登录页面
            response.sendRedirect(request.getContextPath() + "/login.jsp");
            return;
        }
        
        // 转发到申请页面
        request.getRequestDispatcher("/forumApplication.jsp").forward(request, response);
    }
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 获取当前登录用户
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        
        if (user == null) {
            // 用户未登录，重定向到登录页面
            response.sendRedirect(request.getContextPath() + "/login.jsp");
            return;
        }
        
        // 获取表单参数
        String forumName = request.getParameter("forumName");
        String description = request.getParameter("description");
        String reason = request.getParameter("reason");
        
        // 检查板块名称是否已存在
        ForumDao forumDao = new ForumDao();
        if (forumDao.isNameExists(forumName)) {
            request.setAttribute("error", "板块名称已存在，请使用其他名称");
            request.getRequestDispatcher("/forumApplication.jsp").forward(request, response);
            return;
        }
        
        // 检查用户是否已有待处理的创建板块申请
        ForumApplicationDao applicationDao = new ForumApplicationDao();
        if (applicationDao.hasPendingCreateApplication(user.getId())) {
            request.setAttribute("error", "您已经有一个待处理的创建板块申请，请等待审核结果");
            request.getRequestDispatcher("/forumApplication.jsp").forward(request, response);
            return;
        }
        
        // 检查板块名称是否已有待处理的申请
        if (applicationDao.hasPendingForumNameApplication(forumName)) {
            request.setAttribute("error", "该板块名称已有待处理的申请，请使用其他名称");
            request.getRequestDispatcher("/forumApplication.jsp").forward(request, response);
            return;
        }
        
        // 创建申请创建板块的记录
        ForumApplication application = new ForumApplication(forumName, description, user.getId(), reason);
        
        // 保存申请
        int applicationId = applicationDao.createApplication(application);
        
        if (applicationId > 0) {
            request.setAttribute("success", "板块创建申请已提交，请等待管理员审核");
        } else {
            request.setAttribute("error", "申请提交失败，请稍后再试");
        }
        
        // 转发回申请页面
        request.getRequestDispatcher("/forumApplication.jsp").forward(request, response);
    }
}